package config

import (
	"context"
	"time"

	"github.com/go-redis/redis/v8"
)

var RedisClient *redis.Client

func init() {
	client := redis.NewClient(&redis.Options{
		Addr:     "8.155.39.234:6379",
		Password: "xing123",
		DB:       5,
		PoolSize: 10,
	})

	RedisClient = client

	ctx := context.Background()

	client.Set(ctx, "name", "miao", 0).Result()

}

// SetValueByTime
// key : 键
// value : 值
// t : 过期时间
// Unit : 时间单位
func SetValueByTime(key string, value interface{}, t int, Unit time.Duration) error {
	ctx := context.Background()
	exceptTime := Unit * time.Duration(t)
	return RedisClient.Set(ctx, key, value, exceptTime).Err()
}

func ExistKey(key string) bool {
	ctx := context.Background()
	_, err := RedisClient.Exists(ctx, key).Result()
	if err != nil {
		return false
	}
	return true
}

func GetValue(key string) (string, error) {
	ctx := context.Background()
	value, err := RedisClient.Get(ctx, key).Result()
	if err != nil {
		return "", err
	}
	return value, nil
}
